<?php

	// +----------------------------------------------------------------------
	// | 中国省市区三级联动插件
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace addon\china_city\controller;

	use think\facade\Cache;
	use think\facade\Db;
	use yicmf\addon\controller\AdminController;

	class ChinaCity extends AdminController
	{

		public function city()
		{
			if ($this->request->get('city', 0)) {
				$cache_name = 'addon_china_city_city_array_upid_' . $this->request->get('city');
				if (Cache::has($cache_name)) {
					$citys = Cache::get($cache_name);
				} else {
					$citys = Db::name('district')->where('upid', $this->request->get('city'))->where('level', 2)->select();
					Cache::set($cache_name, $citys, 86400);
				}
				$data = [];
				if ($citys) {
					$data[] = [0, '请选择城市'];
					foreach ($citys as $city) {
						$data[] = [$city['id'], $city['name']];
					}
				}

				return json($data);
			} else {
				return json([]);
			}
		}

		public function district()
		{
			if ($this->request->get('district', 0)) {
				$cache_name = 'addon_china_city_district_array_upid_' . $this->request->get('district');
				if (Cache::has($cache_name)) {
					$district = Cache::get($cache_name);
				} else {
					$district = Db::name('district')->where('upid', $this->request->get('district'))->where('level', 3)->select();
					Cache::set($cache_name, $district, 86400);
				}
				$data = [];
				if ($district) {
					$data[] = [0, '请选择区县'];
					foreach ($district as $city) {
						$data[] = [$city['id'], $city['name']];
					}
				}

				return json($data);
			} else {
				return json([]);
			}
		}

		/**
		 * 获取省信息.
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function getProvinces()
		{
			if ($this->request->isAjax()) {
				$pid = $this->request->post('pid');
// 			if(!empty($pid) ){
// 				$map['id'] = $pid;
// 			}
				$map['level'] = 1;
				$map['upid'] = 0;
				$list = Db::name('District')->where($map)->field('id,name')->select();
				$data = "<option value =''>-鐪佷唤-</option>";
				foreach ($list as $k => $vo) {
					$data .= '<option ';
					if ($pid == $vo['id']) {
						$data .= ' selected ';
					}
					$data .= " value ='" . $vo['id'] . "'>" . $vo['name'] . '</option>';
				}

				return json($data);
			}
		}

		/**
		 * 获取城市
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function getCitys()
		{
			if ($this->request->isAjax()) {
				$cid = $this->request->post('cid');
				$pid = $this->request->post('pid');
				if (!empty($cid)) {
					//$map['id'] = $cid;
				}
				$map['level'] = 2;
				$map['upid'] = $pid;
				$list = Db::name('District')->where($map)->field('id,name')->select();
				$data = "<option value =''>-鍩庡競-</option>";
				foreach ($list as $k => $vo) {
					$data .= '<option ';
					if ($cid == $vo['id']) {
						$data .= ' selected ';
					}
					$data .= " value ='" . $vo['id'] . "'>" . $vo['name'] . '</option>';
				}

				return json($data);
			}
		}

		/**
		 * 获取镇信息.
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function getDistricts()
		{
			if ($this->request->isAjax()) {
				$did = $this->request->post('did');
				$cid = $this->request->post('cid');
				if (!empty($did)) {
					//$map['id'] = $did;
				}
				$map['level'] = 3;
				$map['upid'] = $cid;
				$list = Db::name('District')->where($map)->field('id,name')->select();
				$data = "<option value =''>-宸炲幙-</option>";
				foreach ($list as $k => $vo) {
					$data .= '<option ';
					if ($did == $vo['id']) {
						$data .= ' selected ';
					}
					$data .= " value ='" . $vo['id'] . "'>" . $vo['name'] . '</option>';
				}

				return json($data);
			}
		}
	}
